package cn.markers.client.android;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class SplashActivity extends Activity {
	TextView loadStrTextView;

	int count = 0;
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.splash);
		loadStrTextView = (TextView) findViewById(R.id.SplashTextView);
		loadStrTextView.setText("loading...");

		final int welcomeScreenDisplay = 2000;
		Thread welcomeThread = new Thread() {
			SplashActivityActivityHandler handler = new 
					SplashActivityActivityHandler(SplashActivity.this);
			int wait = 0;

			@Override
			public void run() {
				try {
					super.run();
					/**
					 * use while to get the splash time. Use sleep() to increase
					 * the wait variable for every 100L.
					 */
					while (wait < welcomeScreenDisplay) {
						sleep(100);
						wait += 100;
						//Message message = Message.obtain(handler, R.id.splash_update);
						//message.sendToTarget();
					}
				} catch (Exception e) {
					System.out.println("EXc=" + e);
				} finally {
					/**
					 * Called after splash times up. Do some action after splash
					 * times up. Here we moved to another main activity class
					 */
					Intent intent = new Intent(SplashActivity.this,
							CaptureActivity.class);
					startActivity(intent);
					SplashActivity.this.finish();
				}
			}
		};
		welcomeThread.start();
	}

	public final class SplashActivityActivityHandler extends Handler {
		private final SplashActivity activity;
		SplashActivityActivityHandler(SplashActivity activity){
			this.activity = activity;
		}
		public void handleMessage(Message message) {
			switch (message.what) {
			case R.id.splash_update:
				activity.update();
				break;
			}
		}
	}
	public void update() {
		count ++;
		String loadingStr = "loading";
		for(int i = 0;i<(count%4);i++)
			loadingStr += ".";
		loadStrTextView.setText(loadingStr);
	}
}

